﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics.Contracts;

namespace RPGMapRevealer
{
    /// <summary>
    /// Allows configuration of grid settings
    /// </summary>
    public partial class GridConfigUserControl : UserControl
    {
        /// <summary>
        /// Constructs a default grid config user control
        /// </summary>
        public GridConfigUserControl()
        {
            InitializeComponent();
        }

        private void ValueChanged(object sender, EventArgs e)
        {
            _gridConfig.TileWidth = Max((int)tileWidth.Value, 1);
            _gridConfig.TileHeight = Max((int)tileWidth.Value, 1);
            _gridConfig.TileOffsetX = Max((int)tileXOffset.Value, 0);
            _gridConfig.TileOffsetY = Max((int)tileYOffset.Value, 0);
            _gridConfig.TileBorder = Max((int)tileBorder.Value, 0);
        }

        private static int Max(int A, int B)
        {
            Contract.Ensures(Contract.Result<int>() >= B);
            Contract.Ensures(Contract.Result<int>() >= A);
            return A > B ? A : B;
        }

        /// <summary>
        /// The size, offset, and border size of a map
        /// </summary>
        public GridConfig GridConfig
        {
            get
            {
                Contract.Ensures(Contract.Result<GridConfig>() != null);
                return _gridConfig;
            }
            set
            {
                Contract.Requires(value != null);
                _gridConfig = value;
                tileWidth.Value = value.TileWidth;
                tileHeight.Value = value.TileHeight;
                tileXOffset.Value = value.TileOffsetX;
                tileYOffset.Value = value.TileOffsetY;
                tileBorder.Value = value.TileBorder;
            }
        }
        private GridConfig _gridConfig = new GridConfig();

        [ContractInvariantMethod]
        private void InvariantMethod()
        {
            Contract.Invariant(_gridConfig != null);
            Contract.Invariant(toolTips != null);
            Contract.Invariant(groupBoxGridOptions != null);
            Contract.Invariant(label1 != null);
            Contract.Invariant(label2 != null);
            Contract.Invariant(label3 != null);
            Contract.Invariant(label4 != null);
            Contract.Invariant(label5 != null);
            Contract.Invariant(tileWidth != null);
            Contract.Invariant(tileHeight != null);
            Contract.Invariant(tileXOffset != null);
            Contract.Invariant(tileYOffset != null);
            Contract.Invariant(tileBorder != null);
        }
    }
}
